package com.crl.rcdp.sys.dao;

import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysResource;
import com.crl.rcdp.sys.vo.SysResourceSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/22 11:10
 */
@Repository
public class SysResourceDao extends BaseDao<SysResource, SysResourceSearchVO> {
    /**
     * 增加资源权限
     */
    public int add(SysResource sysResource) {
        String sql = "INSERT INTO td_sys_resource(parent_id,resource_name,resource_type,resource_code,display_order,url,target,icon,is_show,status,resource_desc) " +
                "VALUES(:parentId,:resourceName,:resourceType,:resourceCode,:displayOrder,:url,:target,:icon,:isShow,0,:resourceDesc)";
        return insert(sql, sysResource);
    }


    /**
     * 根据id修改
     */
    public int update(SysResource sysResource) {
        String sql = "UPDATE td_sys_resource SET parent_id=:parentId,resource_name=:resourceName," +
                "resource_type=:resourceType,resource_code=:resourceCode,display_order=:displayOrder," +
                "url=:url,target=:target,icon=:icon,is_show=:isShow," +
                "resource_desc=:resourceDesc WHERE id=:id";
        return update(sql, sysResource);
    }

    /**
     * 根据id删除资源
     */
    public int delete(Integer id) {
        String sql = "DELETE FROM td_sys_resource WHERE id=?";
        return delete(sql, id);
    }


    /**
     * 获取所有资源列表
     */
    public List<SysResource> listAll() {
        String sql = "SELECT id,parent_id,resource_name,resource_type,resource_code,display_order,url,target,icon,is_show,status,resource_desc FROM td_sys_resource WHERE status=0 ORDER BY parent_id,display_order";
        return list(sql);
    }

    /**
     * 根据id获取资源
     */
    public SysResource get(Integer id) {
        String sql = "SELECT id,parent_id,(select resource_name from td_sys_resource where id=t.parent_id) parentName," +
                "resource_name,resource_type,resource_code,display_order,url,target,icon,is_show,status,resource_desc FROM td_sys_resource t WHERE id=?";
        return get(sql, id);
    }

    /**
     * 根据资源代码获取资源
     */
    public SysResource getByResourceCode(String sysResourceCode) {
        String sql = "SELECT id,parent_id,resource_name,resource_type,resource_code,display_order,url,target,icon,is_show,status,resource_desc FROM td_sys_resource WHERE resource_code=?";
        return get(sql, sysResourceCode);
    }


    /**
     * 根据id获取下级节点个数
     */
    public int getChildrenCount(Integer parentId) {
        String sql = "SELECT COUNT(id) FROM td_sys_resource WHERE parent_id=?";
        return count(sql, parentId);
    }


    /**
     * 根据userId获取所有用户资源对应
     *
     * @param userId
     * @return
     */
    public List<SysResource> listByUserd(int userId) {
        String sql = "select id,parent_id,resource_name,resource_type,resource_code from td_sys_resource where id in(" +
                " select resource_id from td_sys_role_resource where role_id in(select role_id from td_sys_user_role where user_id=?))";
        return list(sql, userId);
    }

    /**
     * 根据username获取用户具有的url
     *
     * @param username
     * @return
     */
    public List<SysResource> getUserUrls(String username) {
        String sql = "select r.uri from (\n" +
                "select resource_id from td_sys_role_resource where role_id in(\n" +
                "select role_id from td_sys_user_role where user_id in(\n" +
                "select user_id from td_sys_user_login where username=?))) t,td_sys_resource r where t.resource_id=r.id\n" +
                "and r.uri is not null";
        return list(sql, username);
    }
}
